package com.java.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.common.model.Result;
import com.java.common.util.JwtUtil;
import com.java.manage.dao.SupplyMapper;
import com.java.manage.model.AccessoriesType;
import com.java.manage.model.Supply;
import com.java.manage.model.SupplyType;
import com.java.manage.service.SupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplyServiceImpl implements SupplyService {

    @Autowired
    private SupplyMapper supplyMapper;

    @Autowired
    private HttpServletRequest request;

    @Override
    public Result list(Supply supply) {
        PageHelper.startPage(supply.getPageNum(), supply.getPageSize()); // 提醒page在显示哪一页的数据和要显示多少条数据

        String token = request.getHeader("Token");
        Integer storeId = (Integer) JwtUtil.getClaims(token).get("storeId");
        supply.setStoreId(storeId);

        List<Supply> list = supplyMapper.list(supply);
        PageInfo<Supply> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result save(Supply supply) {
        supplyMapper.save(supply);
        return Result.success();
    }

    @Override
    public Result modify(Supply supply) {
        supplyMapper.modify(supply);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            supplyMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result get(int id) {
        supplyMapper.get(id);
        return Result.success();
    }
    @Override
    public Result listSupp() {
        List<Supply> supplys = supplyMapper.listSupp();
        return Result.success(supplys);
    }
}
